package com.xhd.db;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.Date;
import com.xhd.exception.DatabaseException;
import com.xhd.util.DataFilter;

public class DaoValue {
	private static final Log jm9;
	  private static DaoValue jm16;
	  static Class jm10;

	  static
	  {
	    Class tmp3_0 = jm10;
	    if (tmp3_0 == null)
	    {
	     
	      try
	      {
	        DaoValue.jm10 = Class.forName("com.sinocc.systemdbmanager.DaoValue");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp3_0);
	    jm16 = null;
	  }

	  public static DaoValue getInstance()
	  {
	    if (jm16 == null)
	      jm16 = new DaoValue();
	    return jm16;
	  }

	  public String getSQLValue(String paramString, Field paramField, Object paramObject, int paramInt)
	    throws DatabaseException
	  {
	    try
	    {
	      StringBuffer localStringBuffer = new StringBuffer(64);
	      Object localObject;
	      if (paramString.equals("string"))
	      {
	        localObject = paramField.get(paramObject);
	        if (localObject == null)
	        {
	          localStringBuffer.append("''");
	        }
	        else
	        {
	          localStringBuffer.append('\'');
	          localStringBuffer.append(DataFilter.filterStringToDB(localObject));
	          localStringBuffer.append('\'');
	        }
	      }
	      else if (paramString.equals("int"))
	      {
	        localStringBuffer.append(String.valueOf(paramField.getInt(paramObject)));
	      }
	      else if (paramString.equals("bigdecimal"))
	      {
	        localObject = paramField.get(paramObject);
	        if (localObject == null)
	          localStringBuffer.append("0");
	        else
	          localStringBuffer.append(localObject.toString());
	      }
	      else if (paramString.equals("date"))
	      {
	        localObject = (java.util.Date)paramField.get(paramObject);
	        if (localObject == null)
	          return null;
	        SimpleDateFormat localSimpleDateFormat;
	        if (paramInt == 1)
	        {
	          localSimpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	          localStringBuffer.append("to_date('");
	          localStringBuffer.append(localSimpleDateFormat.format((java.util.Date)localObject));
	          localStringBuffer.append("','MM/DD/YYYY HH24:MI:SS')");
	        }
	        else
	        {
	          localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	          localStringBuffer.append('\'');
	          localStringBuffer.append(localSimpleDateFormat.format((java.util.Date)localObject));
	          localStringBuffer.append('\'');
	        }
	      }
	      return localStringBuffer.toString();
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	      throw new DatabaseException(30084, paramField.getName(), localException.getMessage());
	    }
	  }

	  public String getSQLValue(String paramString1, String paramString2, Map paramMap, int paramInt)
	    throws DatabaseException
	  {
	    try
	    {
	      String str = (String)paramMap.get(paramString2);
	      StringBuffer localStringBuffer = new StringBuffer(64);
	      paramString1 = paramString1.toLowerCase();
	      if (paramString1.equals("string"))
	      {
	        if (str == null)
	        {
	          localStringBuffer.append("''");
	        }
	        else
	        {
	          localStringBuffer.append('\'');
	          localStringBuffer.append(DataFilter.filterStringToDB(str));
	          localStringBuffer.append('\'');
	        }
	      }
	      else if (paramString1.equals("int"))
	      {
	        if ((str == null) || (str.length() < 1))
	          localStringBuffer.append("0");
	        else
	          localStringBuffer.append(str);
	      }
	      else if (paramString1.equals("bigdecimal"))
	      {
	        if (str == null)
	          localStringBuffer.append("0");
	        else if (str.length() > 38)
	          localStringBuffer.append(str.subSequence(0, 37));
	        else
	          localStringBuffer.append(str);
	      }
	      else if ((paramString1.equals("date")) || (paramString1.equals("datetime")))
	      {
	        if (str == null)
	          return null;
	        if (paramInt == 1)
	        {
	          Date localDate = new Date(str);
	          SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	          localStringBuffer.append("to_date('");
	          localStringBuffer.append(localSimpleDateFormat.format(localDate));
	          localStringBuffer.append("','MM/DD/YYYY HH24:MI:SS')");
	        }
	        else
	        {
	          localStringBuffer.append('\'');
	          localStringBuffer.append(str);
	          localStringBuffer.append('\'');
	        }
	      }
	      return localStringBuffer.toString();
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	      throw new DatabaseException(30084, paramString2, localException.getMessage());
	    }
	  }
}
